home *** CD-ROM | disk | FTP | other *** search
/ The Arsenal Files 4 / The Arsenal Files 4 (Arsenal Computer).ISO / casm / au116-as.exe / IBMDOS / HANDLE.CPP < prev    next >
C/C++ Source or Header  |  1994-10-24  |  3KB  |  155 lines

  1. #include "..\au.hpp"
  2. #include <fcntl.h>
  3. #include <sys\stat.h>
  4. #include <errno.h>
  5.  
  6. /*****************************************************************************/
  7. /* Replace C's open */
  8.  
  9. STATUS HANDLE::open(AU *au, char *filename, int access)
  10. {
  11.     char string[200];
  12.  
  13.     if (access & O_CREAT)
  14.         fh = ::open(filename, access, S_IWRITE|S_IREAD);
  15.     else
  16.         fh = ::open(filename, access);
  17.  
  18.     if (fh == -1)
  19.     {
  20.         if (_doserrno == ENOENT)      /* file simply missing */
  21.             return FAILURE;
  22.  
  23.         sprintf(string, "Error opening '%s',", filename);
  24.         if (_doserrno == ENOPATH)
  25.         {
  26.             au_printf_error(au, "%s %s", string, "Illegal Path");
  27.             return FAILURE;
  28.         }
  29.         else if (_doserrno == EMFILE)
  30.             au_printf_error(au, "%s %s", string, "Too Many Files Open");
  31.         else if (_doserrno == EACCES)
  32.             au_printf_error(au, "%s %s", string, "Permission Denied");
  33.         return FAILURE;
  34.     }
  35.     reset();
  36.     return SUCCESS;
  37. }
  38.  
  39. /*******************************************************************/
  40. void HANDLE::close()
  41. {
  42.     if (fh >= 0)
  43.         ::close(fh);
  44.     fh = -1;
  45. }
  46.  
  47. /*******************************************************************/
  48. long HANDLE::seek(long pos, int from)
  49. {
  50.     long logical_pos, new_pos;
  51.  
  52.     if (bufferPointer >= bufferEnd)
  53.         return lseek(fh, pos, from);
  54.  
  55.     logical_pos = bufferFilePos + bufferPointer;
  56.  
  57.     if (from == SEEK_END)
  58.     {
  59. #if 0
  60.         /* Work on optimizing this a little later */
  61.         reset();
  62.         return lseek(fh, pos, from);
  63. #endif
  64.  
  65.         new_pos = lseek(fh, pos, from);
  66.         if (new_pos >= bufferFilePos && new_pos <= bufferFilePos + bufferEnd)
  67.         {
  68.             bufferPointer = new_pos - bufferFilePos;
  69.             return new_pos;
  70.         }
  71.         else
  72.         {
  73.             long pos;
  74.  
  75.             pos = new_pos-readBufferSize/2;
  76.             if (pos < 0)
  77.                 pos = 0;
  78.  
  79.             reset();
  80.             lseek(fh, pos, SEEK_SET);
  81.             read_char();         // force it to load in the buffer
  82.             if (new_pos >= bufferFilePos && new_pos <= bufferFilePos + bufferEnd)
  83.             {
  84.                 bufferPointer = new_pos - bufferFilePos;
  85.                 return new_pos;
  86.             }
  87.             else
  88.                 return -1;
  89.         }
  90.     }
  91.     else
  92.     {
  93.         if (from == SEEK_CUR)
  94.             new_pos = logical_pos + pos;
  95.         else
  96.             new_pos = pos;
  97.  
  98.         if (new_pos >= bufferFilePos && new_pos <= bufferFilePos + bufferEnd)
  99.         {
  100.             bufferPointer = new_pos - bufferFilePos;
  101.             return new_pos;
  102.         }
  103.         else
  104.         {
  105.             reset();
  106.             return lseek(fh, new_pos, SEEK_SET);
  107.         }
  108.     }
  109. }
  110. /*******************************************************************/
  111. /* Peel one character off a HANDLE */
  112.  
  113. int HANDLE::read_char()
  114. {
  115.     if (bufferPointer >= bufferEnd)
  116.     {
  117.          bufferFilePos = tell(fh);
  118.          bufferPointer=0;
  119.          bufferEnd = ::read(fh, buffer, readBufferSize);
  120.          if (bufferEnd==0)
  121.              return EOF;
  122.     }
  123.     return (unsigned int)buffer[bufferPointer++];
  124. }
  125.  
  126. /********************************************************************/
  127. void HANDLE::write_text(char *string)
  128. {
  129.     char string2[200];
  130.     int p1=0,p2=0;
  131.     int len = strlen(string);
  132.  
  133.     if (!is_open())
  134.         return;
  135.  
  136.     if (bufferPointer < bufferEnd)
  137.     {
  138.         lseek(fh, bufferFilePos + bufferPointer, SEEK_SET);
  139.     }
  140.  
  141.     while (p1<len)
  142.     {
  143.         if (string[p1] == '\n' && p1 > 0 && string[p1-1] != '\r')
  144.             string2[p2++] = '\r';
  145.         string2[p2++] = string[p1];
  146.         p1++;
  147.     }
  148.     string2[p2] = '\0';
  149.  
  150.     ::write(fh, string2, p2);
  151.     reset();
  152.     return;
  153. }
  154.  
  155.